<template>
    <div style="width: 100%;height: 100%;">
        <iframe v-if="config.ureport" ref="ureport" style="width: 100%;height: 100%;border: none" :src="url"></iframe>
        <span v-else>{{$ti18('noConfigReport','未配置报表!','formCustom')}}</span>
    </div>
</template>

<script>
import FormCustomPublic from "../FormCustomPublic";
import BusEvent from "../BusEvent";
import {TokenUtil} from "@lowcode/jpaas-base-lib";

/**
 * 自定义页面(ureport组件)
 */
export default {
    name: "ureportView",
    mixins:[FormCustomPublic,BusEvent],
    data() {
        return {
            url:"/ureport/preview?_u=qiaolin-",
            //初始化时的url 用于参数拼接时刷新url
            initUrtl:""
        }
    },
    created() {
        this.init();
    },
    methods: {
        init(){
            if(this.config.ureport) {
                let token = TokenUtil.getToken();
                let ureportName = JSON.parse(this.config.ureport).text;
                this.url += ureportName + "&accessToken=" + token;
                this.initUrtl = this.url

                let parameter = {key: this.config.alias, params: '', pmtAlias: this.config.pmtAlias};
                //取路由参数
                let params = this.getRouterParams();
                //读取配置的参数
                for (let i = 0; i < this.config.receive.mapping.length; i++) {
                    let o = this.config.receive.mapping[i];
                    if (o.valueSource == 'param') {
                        if (this.config.receive.type == "url") {
                            if (params[o.valueDef]) {
                                parameter.params += o.name + '=' + params[o.valueDef] + '&';
                            }
                        } else {
                            if (params[o.name]) {
                                parameter.params += o.name + '=' + params[o.name] + '&';
                            }
                        }
                    }
                }
                let self_ = this;
                this.getParams(params,function (params) {
                    for (let key in params) {
                        parameter.params += key + '=' + params[key] + '&';
                    }
                    parameter.params = parameter.params.substring(0, parameter.params.length - 1);
                    if (parameter.params) {
                        self_.initUrtl += "&" + parameter.params;
                    }
                    self_.url = self_.initUrtl
                });
            }
        },
        // 获取路由参数
        getRouterParams() {
            let params = this.$route.params || {};
            if (this.$route.meta.params) {
                params = Object.assign(params, JSON.parse(this.$route.meta.params));
            }
            if (this.$route.query) {
                params = Object.assign(params, this.$route.query);
            }
            return params;
        },
        handPublicReceive(args) {
            let receive = this.config.receive;
            if (this.config.receive.type == 'event' && receive.publishComponent == args.component) {
                let inputParams = args.params;
                let param = "";
                //读取配置的参数
                for (let i = 0; i < receive.mapping.length; i++) {
                    let obj = receive.mapping[i];
                    if (obj.valueSource == 'param' && inputParams[obj.valueDef]) {
                        param += "&" + obj.name + "=" + inputParams[obj.valueDef]
                    }
                }
                this.url = this.initUrtl;
                this.url += param;
            }
        }
    },
    watch: {
        config:{
            handler: function (val, oldVal) {
                if(val){
                    this.url="/ureport/preview?_u=qiaolin-";
                    this.init();
                }
            },
            deep: true
        }
    },
}
</script>

<style scoped>

</style>
